﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using rossisdead.Mvc.Detergent;

namespace DetergentTest
{
    [TestClass]
    public class CustomModelMetadataProviderTest
    {

        [TestMethod]
        public void TestCreateMetadataCallsProcessOnAttributesDerivedFromCustommModelMetadataAttribute()
        {
            var attr = new MockAttribute();
            var attributes = new[] { attr };
            var target = new TestMetaDataProvider();
            target.TestCreateMetadata(attributes, typeof(MockViewModel), null,
                typeof(MockViewModel), "Property");

            Assert.IsTrue(attr.WasProcessed);
        }
        

        #region Helpers
        
        public class TestMetaDataProvider : CustomModelMetadataProvider
        {
            public ModelMetadata TestCreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
            {
                return CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
            }
        }

        public class MockViewModel
        {
            [Mock]
            public object Property { get; set; }
        }

        public class MockAttribute : CustomModelMetadataAttribute
        {
            public bool WasProcessed { get; set; }

            public override void Process(ModelMetadata modelMetaData)
            {
                WasProcessed = true;
            }
        }

        #endregion

    }
}
